/*****************************
 * Copyright (c) 2009 by Hotct Co. Ltd.  All rights reserved.
 ****************************/
package com.happy.common.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @{#} PageTag.java Create on May 6, 2009 10:58:36 AM
 *      <p>
 *      自定义分页标签
 *      </p>
 * @author <a href="mailto:maopenglin@hotmail.com">毛鹏霖</a>
 * @version v 0.1
 */
public class PageTag extends TagSupport {
	private int currentPage = 1;
	private int countPage;
	private String style = "border:1px solid;width:100%";
	private String jsFunction;

	private String selectName = "mys";
	private String url;
    private String space="";
    private String face="楷体";
    private String type="default";
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int doStartTag() throws JspException {
		String opString="";
		String se="";
		try {
			opString+="";
			for(int i=0;i<countPage;i++)
			{
				if(i==(currentPage-1))
					se="selected";
				opString+="<option value="+(i+1)+" "+se+"> "+(i+1)+" </option>";
				se="";
			}
			String selectString="<select name="+selectName+" onchange='"+jsFunction+"(this.options[this.selectedIndex].value)'>"+opString+"</select>";
			if(type.equals("default"))
			{
				if(url.length()<2){
					pageContext.getOut().print(
							"<table style='" + style + "'>" + "<tr width=100%>" + "<td><font size=2 face="+face+">共"+countPage+"页</font></td><td><font size=2 face="+face+">"+space+"当前第"+currentPage+"页</font></td><td>"
							+ ""+space+"<a href='javascript:void(0)' onclick='"+jsFunction+"("+currentPage+")' target=_parent style='text-decoration:none'><font size=2 face="+face+">首页</font></a>"+space+"" + "</td>" + "<td>" + ""+space+"<a <a href='javascript:void(0)' onclick='"+jsFunction+"("+(currentPage-1)+")' target=_parent style='text-decoration:none'><font size=2 face="+face+">上一页</font></a>"+space+""
							+ "</td>" + "<td>" + "<a <a href='javascript:void(0)' onclick='"+jsFunction+"("+(currentPage+1)+")' target=_parent style='text-decoration:none'><font size=2 face="+face+">"+space+"下一页</font></a>"+space+"" + "</td>"
							+ "<td>" + ""+space+"<a href='javascript:void(0)' onclick='"+jsFunction+"("+countPage+")' target=_parent style='text-decoration:none'><font size=2 face="+face+">末页</font></a>"+space+"" + "</td>" + ""+space+"<td><font size=2 face="+face+">"+space+"跳转到第："+selectString+"</font>页</td></tr>"
							+ "</table>");
				}else{
					 pageContext.getOut().print(
								"<table style='" + style + "'>" + "<tr width=100%>" + "<td><font size=2 face="+face+">共"+countPage+"页</font></td><td><font size=2 face="+face+">"+space+"当前第"+currentPage+"页</font></td><td>"
										+ ""+space+"<a href='"+url+"1' target=_parent style='text-decoration:none'><font size=2 face="+face+">首页</font></a>"+space+"" + "</td>" + "<td>" + ""+space+"<a href="+url+""+(geta(getCurrentPage()-1))+" target=_parent style='text-decoration:none'><font size=2 face="+face+">上一页</font></a>"+space+""
										+ "</td>" + "<td>" + "<a href="+url+""+(getc(getCurrentPage()+1))+" target=_parent style='text-decoration:none'><font size=2 face="+face+">"+space+"下一页</font></a>"+space+"" + "</td>"
										+ "<td>" + ""+space+"<a href="+url+""+(countPage)+" target=_parent style='text-decoration:none'><font size=2 face="+face+">末页</font></a>"+space+"" + "</td>" + ""+space+"<td><font size=2 face="+face+">"+space+"跳转到第："+selectString+"</font>页</td></tr>"
										+ "</table>");
					
				}
			}else {
				String abcString="<table><tr>"+getContent()+"</tr></table>";
				pageContext.getOut().print(abcString);
			}
			

		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public int getc(int a) {
		return a > countPage ? countPage : a;
	}

	public int geta(int a) {
		return a > 0 ? a : 1;
	}

	public int getCurrentPage() {
		return currentPage < 0 ? 0 : currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getCountPage() {
		return countPage;
	}

	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getJsFunction() {
		return jsFunction;
	}

	public void setJsFunction(String jsFunction) {
		this.jsFunction = jsFunction;
	}

	public void setSelectName(String selectName) {
		this.selectName = selectName;
	}

	public String getSpace() {
		return space;
	}

	public void setSpace(String space) {
		this.space = space;
	}

	public String getSelectName() {
		return selectName;
	}

	public String getFace() {
		return face;
	}

	public void setFace(String face) {
		this.face = face;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	public String getContent()
	{

		String a="";
		int b=geta(getCurrentPage()+1);;
		a+="<td>"+(geta(getCurrentPage()))+"</td>";
		if(b+1<=getCountPage())
		{
		 a+="<td>"+b+"</td>";
		}
		if(b+1<getCountPage())
		{
			a+="<td><a href="+url+""+(b+1)+">"+(b+1)+"</a></td>";
		}
		if(b+2<getCountPage())
		{
			a+="<td><a href="+url+""+(b+2)+">"+(b+2)+"</a></td>";
		}
		if(b+3<getCountPage())
		{
			a+="<td<a href="+url+""+(b+3)+">"+(b+3)+"</a></td>";
		}
		if(b+4<getCountPage())
		{
			a+="<td><a href="+url+""+(b+4)+">"+(b+4)+"</a></td>";
		}
		if(b+7<getCountPage())
		{
			a+="<td><a href="+url+""+(b+7)+">....</a></td>";
		}
		
		
		a+="<td> <a href="+url+""+getCountPage()+""+getCountPage()+"</a></td>";
		return a;
	}
	
	
}
